<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller
{
    //private $imageData=array();
    public function __construct()
    {
        parent::__construct();
        $this->load->model('image_model');  //加载模板
        $this->db = $this->load->database("default", TRUE);
    }

    public function index()
    {
        $number_sql = "select count(*) as num from u_m_images ";
        $number = $this->db->query($number_sql)->row_array();

        $min_id_sql="select min(id) as min from u_m_images ";
        $min_id = $this->db->query($min_id_sql)->row_array();


        $now_img = $this->input->get('id');
        if(empty($now_img)){
            $now_img=18;
        }

        $ranking_sql = "select * from u_m_grace where image_id=$now_img ORDER BY user_grace ASC  limit 0,5 ";
        $ranking = $this->db->query($ranking_sql)->result_array();  //排行榜

        $imagelist_sql_large =  "select img,id,title,content from u_m_images where id > $now_img limit 0,1 ";
        $imagelist_large = $this->db->query($imagelist_sql_large)->result_array();
        $imagelist_sql_now =  "select img,id,title,content from u_m_images where id = $now_img  ";
        $imagelist_now = $this->db->query($imagelist_sql_now)->result_array();
        $imagelist_sql_minor =  "select img,id,title,content from u_m_images where id < $now_img order by id  DESC  limit 0,1";
        $imagelist_minor = $this->db->query($imagelist_sql_minor)->result_array();
        if(empty($imagelist_minor))
            $imagelist_sql_minor =  "select img,id,title,content from u_m_images where id=(select max(id)  from u_m_images) ";
            $imagelist_minor = $this->db->query($imagelist_sql_minor)->result_array();
        if(empty($imagelist_large))
            $imagelist_sql_large =  "select img,id,title,content from u_m_images where id=(select min(id)  from u_m_images)  ";
            $imagelist_large = $this->db->query($imagelist_sql_large)->result_array();
        $imagelist=[$imagelist_minor[0],$imagelist_now[0],$imagelist_large[0]];
//        print_r($imagelist_minor);
//        print_r($imagelist_large);
//        exit();
        $this->imageData = $this->image_model->select();//调用模板
		$curImg = $this->image_model->select("*","id = {$now_img}");


        $this->images['db'] = $curImg[0];
        $this->images['ranking'] = $ranking;
        $this->images['imagelist'] = $imagelist;

        $this->load->view('puzzle_game', $this->images);   //把数据传到页面

    }

    public function saveData()
    {

        $data = $this->input->post();
        $t = time();
        $data["create_time"] = $t;
        $data["update_time"] = $t;
        $data["create_user"] = 1;
        $data["update_user"] = 1;
        $this->db->insert("u_m_grace", $data);
        echo $this->db->insert_id();
    }

}